using System;
using System.Collections;
using System.Collections.Generic;
using Dto;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Character
{
    public class MyPlayerCtrl : CharacterBase
    {
        private List<CardCtrl> _cardCtrls;
        private Transform _cardParent;

        private void Awake()
        {
            Bind(CharacterEvent.INIT_MY_CARD);
        }

        public override void Execute(int eventCode, object message)
        {
            switch (eventCode)
            {
                case CharacterEvent.INIT_MY_CARD:
                {

                    StartCoroutine(InitCardList(message as List<CardDto>));
                    break;
                }
            }
        }


        private void Start()
        {
            _cardParent = transform.Find("CardPoint");
            _cardCtrls = new List<CardCtrl>();
        }

        private IEnumerator InitCardList(List<CardDto> cards)
        {
            GameObject cardPrefab = Resources.Load<GameObject>("Card/MyCard");

            for (int i = 0; i < cards.Count; i++)
            {
                CreateGo(cards[i], i,cardPrefab);
                yield return new WaitForSeconds(0.1f);
            }
        }
        
        /**
         * 创建卡牌
         */
        private void CreateGo(CardDto card, int index, GameObject cardPrefab)
        {
            GameObject cardGo = Object.Instantiate(cardPrefab,_cardParent) as GameObject;
            cardGo.name = card.name;
            cardGo.transform.localPosition = new Vector2((0.35f * index), 0);

            CardCtrl cardCtrl = cardGo.GetComponent<CardCtrl>();
            cardCtrl.Init(card,index,true);
            // 存储本地
            this._cardCtrls.Add(cardCtrl);
        }
    }
}